import request from '@/utils/request';
import { stringify } from 'qs';
import { RcFile } from 'antd/lib/upload';
import { ProjectListParams, ProjectListItem, ProjectBlueprintUpdateParams } from './data.d';

interface IssueParams {
  pid?: string;
  mid?: string;
  uid?: string;
  bid?: string;
  opinion?: string;
  marks?: MarkPrams[];
}

interface MarkPrams {
  type?: string;
  data: string;
}

export async function queryProject(params?: ProjectListParams) {
  return request('/tsapi/project/list', {
    params,
  });
}

export async function queryProjectById(id: string) {
  return request(`/tsapi/project/info/${id}`);
}

export async function queryUnitById(id: string) {
  return request(`/tsapi/unit/info/${id}`);
}

export async function queryMajorListByUid(uid: string) {
  return request(`/tsapi/major/infoList/${uid}`);
}

export async function queryProjectUnitList(params: { pid: string }) {
  return request(`/tsapi/unit/infoList/${params.pid}`);
}

export async function saveUnit(params: { pid: string; mid: string; name: string }) {
  return request('/tsapi/unit/create', {
    method: 'POST',
    data: params,
  });
}

export async function removeProject(params: { id: string[] }) {
  return request('/tsapi/project/delete', {
    method: 'POST',
    data: {
      ...params,
      method: 'delete',
    },
  });
}

export async function addProject(params: ProjectListItem) {
  return request('/tsapi/project/save', {
    method: 'POST',
    body: JSON.stringify(params),
  });
}

export async function updateProject(params: ProjectListParams) {
  return request('/api/project', {
    method: 'POST',
    data: {
      ...params,
      method: 'update',
    },
  });
}

/**
 * 项目图纸信息
 * @param params
 */
export async function updateProjectBlueprint(params: ProjectBlueprintUpdateParams) {
  return request('/api/blueprint', {
    method: 'POST',
    data: {
      ...params,
      method: 'post',
    },
  });
}

export async function addBlueprint(params: {
  id?: string;
  mid: string;
  name: string;
  url: string;
}) {
  return request('/tsapi/blueprint/create', {
    method: 'POST',
    data: params,
  });
}

/**
 * 项目图纸信息
 * @param params
 */
export async function queryProjectBlueprint(params: { pid: string; mid: string; uid: string }) {
  return request(`/tsapi/blueprint/info/${params.pid}/${params.mid}/${params.uid}`);
}

export async function queryBlueprintListByMid(mid: string) {
  return request(`/tsapi/blueprint/infoList/${mid}`);
}

export async function queryBlueprintHistoryByMid(mid: string) {
  return request(`/tsapi/blueprint/historyInfoList/${mid}`);
}

export async function queryBlueprintById(id: string) {
  return request(`/tsapi/blueprint/info/${id}`);
}

export async function queryBlueprintHistoryListByBid(bid: string) {
  return request(`/tsapi/blueprinthistory/list?bid=${bid}`);
}

export async function queryBlueprintHistoryCompareInfo(params: { mId: string; oId: string }) {
  return request(`/tsapi/blueprinthistory/compareInfo/${params.mId}/${params.oId}`);
}

export async function uploadPdf(file: RcFile) {
  const formData = new FormData();
  formData.append('file', file, file.name);
  return request('/tsapi/file/upload', {
    method: 'POST',
    body: formData,
  });
}

export async function createIssue(params: IssueParams) {
  return request('/tsapi/issue/create', {
    method: 'POST',
    data: params,
  });
}

export async function queryIssue(params: IssueParams) {
  return request(`/tsapi/issue/list?${stringify(params)}`);
}

export async function queryBlueprintIssue(params: IssueParams) {
  return request(`/tsapi/blueprinthistory/list/issue?${stringify(params)}`);
}

export async function pdfCompare(params: { mUrl: string; oUrl: string }) {
  return request('/tsapi/file/compare', {
    method: 'POST',
    data: params,
  });
}

export async function getBase64String(params: { mUrl: string; oUrl: string }) {
  return request(`/tsapi/file/getBase64String?${stringify(params)}`, {
    timeout: 0,
  });
}

export async function invocations(params: { m: string; o: string }) {
  return request('/t3api/invocations', {
    method: 'POST',
    timeout: 0,
    headers: {
      'Content-Type': 'application/json; format=pandas-split',
      Accept: 'application/json',
    },
    body: JSON.stringify({
      columns: ['pdf_m', 'pdf_o'],
      index: [0],
      data: [[params.m, params.o]],
    }).replaceAll('/', '\\/'),
  });
}
